package com.bole.controller;

import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.permission.AdminDto;
import com.bole.dto.permission.RoleDto;
import com.bole.pojo.TbAdminDo;
import com.bole.pojo.TbRoleDo;
import com.bole.service.AdminService;
import com.bole.service.RoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author yakamaxi
 * @version 1.0
 * @date 2020/8/12 13:29
 */
@RestController
@RequestMapping("admin")
@Api("用户管理接口")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result get() {
        List<TbAdminDo> adminDoList = adminService.getInfo();
        ArrayList<AdminDto> adminDtoList = new ArrayList<>();
//        adminDoList.forEach(x -> BeanUtils.copyProperties(x, adminDto));
        // 转为 dto
        for (TbAdminDo tbAdminDo : adminDoList) {
            AdminDto adminDto = new AdminDto();
            BeanUtils.copyProperties(tbAdminDo, adminDto);
            adminDtoList.add(adminDto);
        }
        return Result.response(ResultEnum.OK).setData(adminDtoList);
    }

    @GetMapping("/byName")
    public Result getByName(@RequestParam("username") String username) {
        TbAdminDo admin = adminService.getInfoByName(username);
        // 这里要先判断不然后面两行会报错 !!
        if (admin == null) {
            return Result.response(ResultEnum.FAIL);
        }
        AdminDto adminDto = new AdminDto();
        BeanUtils.copyProperties(admin, adminDto);
        return Result.response(ResultEnum.OK).setData(adminDto);
    }

    // rid
    @PostMapping
    public Result add(@RequestParam("rid") Long rid, @RequestBody TbAdminDo adminDo) {
        adminService.add(adminDo, rid);
        return Result.response(ResultEnum.OK);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        // 查询用户
        TbAdminDo admin = adminService.getAdminById(id);
        // 查询所有角色
        List<TbRoleDo> roleList = roleService.getInfo();
        // 查询当前用户对应的角色id
        Long rid = roleService.findRoleByAdminId(id);
        // TbAdminDo 转 dto
        AdminDto adminDto = new AdminDto();
        BeanUtils.copyProperties(admin, adminDto);
        // roleList 转 dto
        List<RoleDto> roleDtoList = new ArrayList<>();
        for (TbRoleDo tbRoleDo : roleList) {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(tbRoleDo, roleDto);
            roleDtoList.add(roleDto);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("admin", adminDto);
        map.put("roleList", roleDtoList);
        map.put("rid", rid);
        return Result.response(ResultEnum.OK).setData(map);
    }

    @PutMapping("/{id}")
    public Result updateById(@PathVariable("id") Long id, @RequestParam("rid") Long rid, @RequestBody TbAdminDo admin) {
        admin.setId(id);
        adminService.updateById(admin, rid);
        return Result.response(ResultEnum.OK);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        adminService.deleteById(id);
        return Result.response(ResultEnum.OK);
    }

}
